package com.jt.aspect;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jt.anno.RequireLog;
import com.jt.pojo.Log;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.lang.reflect.Method;
import java.util.Date;

@Aspect
@Component
public class SysLogAspect {
    @Pointcut("@annotation(com.jt.anno.RequireLog)")
    public void doLog(){
        //此方法中不需要写任何内容,只负责承载@Pointcut注解
    }
    @Around("doLog()")
    public Object doAround(ProceedingJoinPoint joinPoint)throws Throwable{
        int status=1;//状态
        String error=null;//错误信息
        long time=0l;//执行时长
        long t1=System.currentTimeMillis();
        try {
            //手动调用目标执行链(这个执行链中包含切入点方法~目标方法)
            Object result = joinPoint.proceed();
            long t2=System.currentTimeMillis();
            time=t2-t1;
            return result;
        }catch (Throwable e){
            long t3=System.currentTimeMillis();
            time=t3-t1;
            status=0;
            error=e.getMessage();
            throw e;
        }finally {
            saveLog(joinPoint,time,status,error);
        }
    }

    private void saveLog(ProceedingJoinPoint joinPoint, long time, int status, String error) throws NoSuchMethodException, JsonProcessingException {
        //1.获取用户行为日志
        //1.1获取目标对象类型(切入点方法所在类的类型)
        Class<?> targetClass = joinPoint.getTarget().getClass();
        //1.2.获取目标方法
        //1.2.1获取方法签名(包含方法信息,....)
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        //1.2.2获取方法对象
        Method targetMethod=
                targetClass.getDeclaredMethod(signature.getName(),signature.getParameterTypes());
        //1.3获取方法上的RequiredLog注解内容
        //1.3.1获取目标方法上注解
        RequireLog requiredLog=targetMethod.getAnnotation(RequireLog.class);
        //1.3.2获取注解中的内容(这个内容为我们定义的操作名)
        String operation=requiredLog.value();
        //1.4获取目标方法名(类名+方法名)
        String targetMethodName=targetClass.getName()+"."+targetMethod.getName();
        //1.5获取目标方法执行时传入的参数
        String params=new ObjectMapper().writeValueAsString(joinPoint.getArgs());




        //1.7获取ip地址(从当前线程获取request对象,然后基于request获取ip地址)
        //String ip="192.168.1.100";
        ServletRequestAttributes requestAttributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String ip=requestAttributes.getRequest().getRemoteAddr();
        //2.将用户行为日志,封装到Log对象
        Log logInfo=new Log();
        logInfo.setIp(ip);//后续获取
        logInfo.setOperation(operation);
        logInfo.setMethod(targetMethodName);
        logInfo.setParams(params);
        logInfo.setTime(time);
        logInfo.setCreatedTime(new Date());
        System.out.println("logInfo="+logInfo);

    }


}
